﻿using EventMgrLib;
using SmartMotion.Core;
using SmartMotion.Properties;
using Sunny.UI;
using System;
using System.Threading;
using SmartMotion.AllPages;
using SmartMotion.AllPages.Debug;
using SmartMotion.AllPages.SysConfig;
using SmartMotion.Pages;


namespace SmartMotion
{
    public partial class FrmMain : UIForm
    {
        private PagesMgr _pagesMgr;
        private EventMgr _em;
        private UnityIoc _ioc;
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            _ioc = UnityIoc.Ins;
            _em = EventMgr.Ins;
            IniUI();
            _em.GetEvent<GlobalControlEvent>().Subscribe(GlobalControlAction);
        }

        private void GlobalControlAction(Controller controller)
        {
            switch (controller)
            {
                case Controller.None:
                    break;
                case Controller.Initialize:
                    break;
                case Controller.Home:
                    break;
                case Controller.Start:
                   
                    break;
                case Controller.Pause:
                    break;
                case Controller.Continue:
                    break;
                case Controller.Stop:
                    break;
                case Controller.RunTool:
                    break;
                case Controller.RunFlow:
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(controller), controller, null);
            }

            //ShowInfoDialog(controller.ToString());
        }   

        private void IniUI()
        {
            ShowDragStretch = true;
            if (Settings.Default.width > 0) Width = Settings.Default.width;
            if (Settings.Default.height > 0) Height = Settings.Default.height;
            if (Settings.Default.top >= 0) Top = Settings.Default.top;
            if (Settings.Default.left >= 0) Left = Settings.Default.left;
            //获取单例模式
            _pagesMgr = PagesMgr.Ins;
            //注册容器
            _pagesMgr.RegisterPanel(panMain);
            //注册选项卡切换页面
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<UIPage>("FrmAuto"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<UIPage>("FrmManual"));

            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmProductMgr>());
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmFlowEdit>());
            //注册按钮切换手动调试页面
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmStationDebug>());
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmIODebug>());
        
            //注册按钮切换系统配置页面
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmMotionConfig>());
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmIOConfig>());
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmStationConfig>());
            _pagesMgr.RegisterPageToPanel(panMain.Name, _ioc.Resolve<FrmDeviceConfig>());

            //显示自动运行页面
            _pagesMgr.ShowPage(panMain.Name, "FrmAuto");
        }

        private void tabRibbon_Selected(object sender, System.Windows.Forms.TabControlEventArgs e)
        {
            if (e.TabPage.Tag == null) return;
            string pageName = e.TabPage.Tag.ToString();
            _pagesMgr.ShowPage(panMain.Name, pageName);
        }

        private void NaviButton_Click(object sender, EventArgs e)
        {
            if (!(sender is UIHeaderButton button)) return;
            if (button.Tag != null)
            {
                _pagesMgr.ShowPage(panMain.Name, button.Tag?.ToString());
            }
        }

        private void FrmMain_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            #region SaveUISet

            Settings.Default.width = Width;
            Settings.Default.height = Height;
            Settings.Default.top = Top;
            Settings.Default.left = Left;
            Settings.Default.Save();

            #endregion SaveUISet
        }

        private void GlobalControlButton_Click(object sender, EventArgs e)
        {
            if (!(sender is UIHeaderButton button)) return;

            var controller = Controller.None;
            if (button == btnStart)
            {
                controller = Controller.Start;
                _em.GetEvent<DebugMessage>().Publish("启动自动运行流程!");
            }

            if (button == btnHome)
            {
                controller = Controller.Home;
                _em.GetEvent<DebugMessage>().Publish("开始回零!");
            }

            if (button == btnPause)
            {
                controller = Controller.Pause;
            }

            if (button == btnContinue)
            {
                controller = Controller.Continue;
            }

            if (button == btnStop)
            {
                controller = Controller.Stop;
            }

            if (button == btnRunSelected)
            {
                controller = Controller.RunTool;
            }

            if (button == btnRunFlow)
            {
                controller = Controller.RunFlow;
            }

            _em.GetEvent<GlobalControlEvent>().Publish(controller);
        }

        private void FrmMain_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            if (!ShowAskDialog("退出程序","是否退出程序"))
            {
                e.Cancel = true;
            }
            else
            {
                _em.GetEvent<ExitApp>().Publish();
                Thread.Sleep(1000);
                _em.GetEvent<GlobalControlEvent>().Unsubscribe(GlobalControlAction);
            }
        }

        #region 左键菜单显示IDE窗口

        private void btnWindows_Click(object sender, EventArgs e)
        {
            btnWindows.ShowContextMenuStrip(LeftButtonMenu, 0, btnWindows.Height);
        }

        private void 工具箱ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _em.GetEvent<ShowWindow>().Publish(WindowName.ToolBox);
        
        }

        private void 资源管理器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _em.GetEvent<ShowWindow>().Publish(WindowName.Solution);
        }

        private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _em.GetEvent<ShowWindow>().Publish(WindowName.Property);
        }

        private void 输出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _em.GetEvent<ShowWindow>().Publish(WindowName.Debug);
        }

        private void 变量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _em.GetEvent<ShowWindow>().Publish(WindowName.Var);
        }

        private void 参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _em.GetEvent<ShowWindow>().Publish(WindowName.Parms);
        }
            
        private void 默认布局ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _em.GetEvent<ShowWindow>().Publish(WindowName.Default);
        }

        #endregion

        #region 流程编辑界面导航

        private void btnSaveFlow_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.Save);
        }

        private void btnNewFlow_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.Create);
        }

        private void btnOpenFlow_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.Open);
        }

        private void btnFlowSaveAs_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.SaveAs);
        }

        #endregion

        private void ProductMgr(object sender, EventArgs e)
        {
            var button = sender as UIHeaderButton;
            if (button == null) return;
            var fileOpt = FileOpt.None;
            if (button == btnProductOpen)
            {
                fileOpt = FileOpt.Create;
            }
            if (button == btnProductSave)
            {
                fileOpt = FileOpt.Save;
            }
            if (button == btnProductSaveAs)
            {
                fileOpt = FileOpt.SaveAs;
            }
            if (button == btnProductOpen)
            {
                fileOpt = FileOpt.Open;
            }
            if (button == btnProductAddPar)
            {
                fileOpt = FileOpt.Add;
            }
            if (button == btnProductParDel)
            {
                fileOpt = FileOpt.Del;
            }
            _em.GetEvent<ProductEvent>().Publish(fileOpt);
        }
    }
}